from itertools import product
from math import factorial
from math import prod


class Solution:
    def getProbability(self, balls):
        k, n, q = len(balls), sum(balls) // 2, 0

        # 生成每一个球所有可能的分布数
        arrays = [range(0, i + 1) for i in balls]

        # 计算所有球的分布的笛卡尔积（左侧盒子中球的分布）
        t = list(product(*arrays))

        # 遍历所有可能的左侧盒子中球的分布
        for i in range(len(t)):
            if sum(t[i]) == n:  # 判断左侧盒子中的球是否为一半
                if t[i].count(0) == t[-i - 1].count(0):  # t[-i-1]是t[i]的另一个盒子
                    q += self.multinomial(t[i]) * self.multinomial(t[-i - 1])  # 计算当前分布的所有可能数

        return q / self.multinomial(list(balls))

    @staticmethod
    def multinomial(n):
        return factorial(sum(n)) / prod([factorial(i) for i in n])


if __name__ == "__main__":
    print(Solution().getProbability(balls=[1, 1]))  # 1.00000
    print(Solution().getProbability(balls=[2, 1, 1]))  # 0.66667
    print(Solution().getProbability(balls=[1, 2, 1, 2]))  # 0.60000
    print(Solution().getProbability(balls=[3, 2, 1]))  # 0.30000
    # print(Solution().getProbability(balls=[6, 6, 6, 6, 6, 6]))  # 0.90327
